import grails.test.GrailsUnitTestCase
import FinalProject.Platform
import FinalProject.Game
import FinalProject.Review
import FinalProject.SiteUser

/**
 * Created with IntelliJ IDEA.
 * User: jacksonmelius
 * Date: 12/14/12
 * Time: 12:40 AM
 * To change this template use File | Settings | File Templates.
 */
class searchServiceTests extends GrailsUnitTestCase{
    def searchService

    void testSearchGame() {
        def p1 = new Platform(name: "SNES", dateOfRelease: "1990")
        def g1 = new Game(name: "Link to the Past", releaseDate: "1991")
        def u1 = new SiteUser(name: "TheTerminator", dateJoined: new Date())
        p1.addToGames(g1)
        p1.save(failOnError: true)
        u1.post("One of my favorite Zelda games!", g1)
        u1.save(failOnError: true)
        def query = "Link"
        def results = searchService.searchGame(query)
        assertNotNull(results)
        assertEquals(results[0].name, "Link to the Past")
        assertEquals(results[0].platform, p1)
    }

    void testSearchPlatform() {
        def p1 = new Platform(name: "SNES", dateOfRelease: "1990")
        def p2 = new Platform(name: "Wii", dateOfRelease:  "2006")
        def p3 = new Platform(name: "XBox 360", dateOfRelease: "2005")
        def g1 = new Game(name: "Super Metroid", releaseDate: "1993")
        def g2 = new Game(name: "Dead Rising", releaseDate:  "2006")
        def g3 = new Game(name: "Super Smash Bros Brawl", releaseDate:  "March 2008")
        p1.addToGames(g1)
        p2.addToGames(g3)
        p3.addToGames(g2)
        p1.save(failOnError:true)
        p2.save(failOnError:true)
        p3.save(failOnError:true)
        def query = "XBox"
        def results = searchService.searchPlatform(query)
        assertNotNull(results)
        assertEquals(results[0].name, "XBox 360")
        assertFalse(results[0].games.isEmpty())
    }

    void testSearchUser() {
        def u1 = new SiteUser(name: "TheTerminator", dateJoined: new Date())
        def u2 = new SiteUser(name: "The Terminator", dateJoined:  new Date())
        def u3 = new SiteUser(name: "The Bearminator", dateJoined: new Date())
        def p1 = new Platform(name: "SNES", dateOfRelease: "1990")
        def p2 = new Platform(name: "XBox 360", dateOfRelease: "2005")
        def g1 = new Game(name: "Super Metroid", releaseDate: "1993")
        def g2 = new Game(name: "Dead Rising", releaseDate:  "2006")
        p1.addToGames(g1)
        p2.addToGames(g2)
        p1.save(failOnError: true)
        p2.save(failOnError: true)
        u1.post("LOVE IT LOVE IT LOVE IT!", g1)
        u1.post("Slaying zombies is fun!", g2)
        u2.post("HATE IT HATE IT HATE IT!", g2)
        u3.post("AMBIVALENT ABOUT IT AMBIVALENT ABOUT IT", g2)
        u1.save(failOnError:true)
        u2.save(failOnError:true)
        u3.save(failOnError: true)
        def query = "Terminator"
        def results = searchService.searchUser(query)
        assertEquals(2, results.size())
        assertEquals(2, results[0].reviews.size())
        def results2 = searchService.searchUser("Bear")
        assertEquals(1, results2.size())

    }
}
